/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package View.venta;

import Controller.MovimientoC;
import Controller.TiendaC;
import Controller.VentaDetalleC;
import Model.MovimientoM;
import Model.TiendaM;
import Util.ArrayListTableModel;
import Util.AutoJComboBox;
import Util.MiRenderBoton;
import View.gestion.frmModelo;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.ArrayList;
import java.util.List;
import java.util.Locale;
import java.util.Random;
import javax.swing.JOptionPane;
import javax.swing.ListSelectionModel;
import javax.swing.SwingConstants;
import javax.swing.table.DefaultTableCellRenderer;

/**
 *
 * @author Maryluz
 */
public class frmMovimientoAlmacen extends javax.swing.JDialog {

    String[] tiendaEmisor;
    String[] tiendaReceptor;
    String[][] oList;
    int xidTienda;
    TiendaM oTienda;
    MovimientoM oMaterial;
    Date ofechaDate;
    public static ArrayListTableModel oModelTblas;

    public frmMovimientoAlmacen(java.awt.Frame parent, boolean modal, String fecha, String user, TiendaM oM) {
        super(parent, modal);
        cargarTiendas();
        initComponents();
        ofechaDate = new Date();
        configurarTabla();
        txtUsuario.setText(oM.getNombres());
        txtFecha.setText(fecha);

        tblEmisor.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
        tblReceptor.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
        this.setLocationRelativeTo(null);
        this.setResizable(false);
        oTienda = oM;
        xidTienda = oM.getIdTienda();
        System.out.println(xidTienda + "");
        oModelTblas = (ArrayListTableModel) tblReceptor.getModel();
        tblEmisor.setRowHeight(45);
        tblReceptor.setRowHeight(45);
        cbnEmite.requestFocus();

    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        btnNueva = new javax.swing.JButton();
        btnHistorial = new javax.swing.JButton();
        cbnEmite = new AutoJComboBox(tiendaEmisor);
        jLabel1 = new javax.swing.JLabel();
        jLabel2 = new javax.swing.JLabel();
        jLabel3 = new javax.swing.JLabel();
        cbnRecepciona = new AutoJComboBox(tiendaReceptor);
        jPanel1 = new javax.swing.JPanel();
        jLabel7 = new javax.swing.JLabel();
        jLabel4 = new javax.swing.JLabel();
        jScrollPane1 = new javax.swing.JScrollPane();
        tblEmisor = new javax.swing.JTable();
        jScrollPane2 = new javax.swing.JScrollPane();
        tblReceptor = new javax.swing.JTable();
        btnListar = new javax.swing.JButton();
        btnRegistrar = new javax.swing.JButton();
        jSeparator2 = new javax.swing.JSeparator();
        jLabel5 = new javax.swing.JLabel();
        txtUsuario = new javax.swing.JTextField();
        jLabel6 = new javax.swing.JLabel();
        cbnTipo = new javax.swing.JComboBox();
        txtFecha = new javax.swing.JTextField();
        jLabel8 = new javax.swing.JLabel();
        btnOcurrencias = new javax.swing.JButton();
        jButton1 = new javax.swing.JButton();

        btnNueva.setFont(new java.awt.Font("Tahoma", 1, 11)); // NOI18N
        btnNueva.setIcon(new javax.swing.ImageIcon(getClass().getResource("/Image/registro.png"))); // NOI18N
        btnNueva.setText("<html> &nbsp; &nbsp; NUEVA <br> &nbsp; &nbsp; PRENDA</html>");
        btnNueva.setActionCommand("<html> &nbsp; &nbsp; NUEVA <br>&nbsp; &nbsp;PRENDA</html>");
        btnNueva.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnNuevaActionPerformed(evt);
            }
        });

        btnHistorial.setFont(new java.awt.Font("Tahoma", 1, 11)); // NOI18N
        btnHistorial.setIcon(new javax.swing.ImageIcon(getClass().getResource("/Image/url_historial.png"))); // NOI18N
        btnHistorial.setText("<html> &nbsp; HISTORIAL</html>");

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
        setTitle("Movimientos de Almacen");
        setResizable(false);

        cbnEmite.setFont(new java.awt.Font("Calibri", 1, 14)); // NOI18N
        cbnEmite.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                cbnEmiteActionPerformed(evt);
            }
        });

        jLabel1.setFont(new java.awt.Font("Calibri", 1, 14)); // NOI18N
        jLabel1.setText("Tienda Origen:");

        jLabel2.setFont(new java.awt.Font("Tahoma", 2, 11)); // NOI18N
        jLabel2.setText("Tienda que emite el envio ");

        jLabel3.setFont(new java.awt.Font("Calibri", 1, 14)); // NOI18N
        jLabel3.setText("Tienda Destino:");

        cbnRecepciona.setFont(new java.awt.Font("Calibri", 1, 14)); // NOI18N
        cbnRecepciona.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                cbnRecepcionaActionPerformed(evt);
            }
        });

        jPanel1.setBackground(new java.awt.Color(0, 0, 0));
        jPanel1.setBorder(javax.swing.BorderFactory.createEtchedBorder());
        jPanel1.setForeground(new java.awt.Color(255, 255, 255));

        jLabel7.setFont(new java.awt.Font("Calibri", 1, 26)); // NOI18N
        jLabel7.setForeground(new java.awt.Color(255, 255, 255));
        jLabel7.setText("MOVIMIENTOS DE ALMACEN");

        javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
        jPanel1.setLayout(jPanel1Layout);
        jPanel1Layout.setHorizontalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel1Layout.createSequentialGroup()
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addComponent(jLabel7)
                .addGap(198, 198, 198))
        );
        jPanel1Layout.setVerticalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addGap(26, 26, 26)
                .addComponent(jLabel7, javax.swing.GroupLayout.PREFERRED_SIZE, 33, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(32, Short.MAX_VALUE))
        );

        jLabel4.setFont(new java.awt.Font("Tahoma", 2, 11)); // NOI18N
        jLabel4.setText("Tienda que recepciona el envio ");

        tblEmisor.setFont(new java.awt.Font("Calibri", 0, 16)); // NOI18N
        tblEmisor.setModel(new ArrayListTableModel(
            new Object [][] {},
            new String [] {" STOCK"," DESCRIPCIÓN"," PRECIO",""}
        ) {
            Class[] types = new Class [] {
                java.lang.String.class, java.lang.String.class, java.lang.String.class, java.lang.String.class
            };
            boolean[] canEdit = new boolean [] {
                false,false,false,false
            };

            public Class getColumnClass(int columnIndex) {
                return types [columnIndex];
            }

            public boolean isCellEditable(int rowIndex, int columnIndex) {
                return canEdit [columnIndex];
            }
        });
        tblEmisor.setCursor(new java.awt.Cursor(java.awt.Cursor.DEFAULT_CURSOR));
        tblEmisor.getTableHeader().setReorderingAllowed(false);
        tblEmisor.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                tblEmisorMouseClicked(evt);
            }
        });
        tblEmisor.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyReleased(java.awt.event.KeyEvent evt) {
                tblEmisorKeyReleased(evt);
            }
        });
        jScrollPane1.setViewportView(tblEmisor);

        tblReceptor.setFont(new java.awt.Font("Calibri", 0, 16)); // NOI18N
        tblReceptor.setModel(new ArrayListTableModel(
            new Object [][] {},
            new String [] {""," DESCRIPCIÓN"," ENVIAR"," STOCK","",""}
        ) {
            Class[] types = new Class [] {
                java.lang.String.class, java.lang.String.class, java.lang.String.class, java.lang.String.class, java.lang.String.class
            };
            boolean[] canEdit = new boolean [] {
                false,false,false,false,false,false
            };

            public Class getColumnClass(int columnIndex) {
                return types [columnIndex];
            }

            public boolean isCellEditable(int rowIndex, int columnIndex) {
                return canEdit [columnIndex];
            }
        });
        tblReceptor.setRowSelectionAllowed(false);
        tblReceptor.getTableHeader().setReorderingAllowed(false);
        tblReceptor.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                tblReceptorMouseClicked(evt);
            }
        });
        jScrollPane2.setViewportView(tblReceptor);

        btnListar.setFont(new java.awt.Font("Tahoma", 1, 11)); // NOI18N
        btnListar.setIcon(new javax.swing.ImageIcon(getClass().getResource("/Image/ROPA.png"))); // NOI18N
        btnListar.setToolTipText("Stock");
        btnListar.setActionCommand("<HTML>STOCK DE LA<BR> TIENDA DESTINO</HTML>");
        btnListar.setEnabled(false);
        btnListar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnListarActionPerformed(evt);
            }
        });

        btnRegistrar.setFont(new java.awt.Font("Tahoma", 1, 11)); // NOI18N
        btnRegistrar.setIcon(new javax.swing.ImageIcon(getClass().getResource("/Image/CashBox Register-32.png"))); // NOI18N
        btnRegistrar.setText("REGISTRAR ENVIO");
        btnRegistrar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnRegistrarActionPerformed(evt);
            }
        });

        jLabel5.setFont(new java.awt.Font("Calibri", 1, 16)); // NOI18N
        jLabel5.setForeground(new java.awt.Color(102, 0, 0));
        jLabel5.setText("USUARIO ");

        txtUsuario.setEditable(false);
        txtUsuario.setFont(new java.awt.Font("Calibri", 1, 16)); // NOI18N
        txtUsuario.setBorder(javax.swing.BorderFactory.createEmptyBorder(1, 1, 1, 1));

        jLabel6.setFont(new java.awt.Font("Calibri", 1, 16)); // NOI18N
        jLabel6.setForeground(new java.awt.Color(102, 0, 0));
        jLabel6.setText("TIPO ");

        cbnTipo.setFont(new java.awt.Font("Calibri", 1, 16)); // NOI18N
        cbnTipo.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "ENTREGAS", "DEVOLUCIONES" }));
        cbnTipo.setFocusable(false);

        txtFecha.setEditable(false);
        txtFecha.setFont(new java.awt.Font("Calibri", 1, 16)); // NOI18N
        txtFecha.setHorizontalAlignment(javax.swing.JTextField.RIGHT);
        txtFecha.setToolTipText("");
        txtFecha.setBorder(javax.swing.BorderFactory.createEmptyBorder(1, 1, 1, 1));

        jLabel8.setFont(new java.awt.Font("Calibri", 1, 16)); // NOI18N
        jLabel8.setForeground(new java.awt.Color(102, 0, 0));
        jLabel8.setText("FECHA ");

        btnOcurrencias.setFont(new java.awt.Font("Tahoma", 1, 11)); // NOI18N
        btnOcurrencias.setIcon(new javax.swing.ImageIcon(getClass().getResource("/Image/maintenance_time.png"))); // NOI18N
        btnOcurrencias.setText("OCURRENCIAS");
        btnOcurrencias.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnOcurrenciasActionPerformed(evt);
            }
        });

        jButton1.setFont(new java.awt.Font("Tahoma", 1, 11)); // NOI18N
        jButton1.setIcon(new javax.swing.ImageIcon(getClass().getResource("/Image/Pedido.png"))); // NOI18N
        jButton1.setText("<HTML>CONSULTAR<BR> MOVIMIENTOS</HTML>");
        jButton1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton1ActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jPanel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jSeparator2)
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(jLabel5)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addComponent(txtUsuario, javax.swing.GroupLayout.DEFAULT_SIZE, 177, Short.MAX_VALUE)
                        .addGap(18, 18, 18)
                        .addComponent(jLabel6)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addComponent(cbnTipo, javax.swing.GroupLayout.PREFERRED_SIZE, 133, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGap(42, 42, 42)
                        .addComponent(jLabel8)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(txtFecha, javax.swing.GroupLayout.PREFERRED_SIZE, 187, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addGroup(layout.createSequentialGroup()
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(jLabel2)
                            .addGroup(layout.createSequentialGroup()
                                .addComponent(jLabel1)
                                .addGap(41, 41, 41)
                                .addComponent(cbnEmite, javax.swing.GroupLayout.PREFERRED_SIZE, 151, javax.swing.GroupLayout.PREFERRED_SIZE))
                            .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 357, javax.swing.GroupLayout.PREFERRED_SIZE))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(jLabel4, javax.swing.GroupLayout.Alignment.TRAILING)
                            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                                .addComponent(jLabel3)
                                .addGap(18, 18, 18)
                                .addComponent(cbnRecepciona, javax.swing.GroupLayout.PREFERRED_SIZE, 151, javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                .addComponent(btnListar, javax.swing.GroupLayout.PREFERRED_SIZE, 62, javax.swing.GroupLayout.PREFERRED_SIZE))
                            .addComponent(jScrollPane2, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.PREFERRED_SIZE, 357, javax.swing.GroupLayout.PREFERRED_SIZE))))
                .addContainerGap())
            .addGroup(layout.createSequentialGroup()
                .addGap(80, 80, 80)
                .addComponent(btnOcurrencias, javax.swing.GroupLayout.PREFERRED_SIZE, 161, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(49, 49, 49)
                .addComponent(btnRegistrar, javax.swing.GroupLayout.PREFERRED_SIZE, 182, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(49, 49, 49)
                .addComponent(jButton1, javax.swing.GroupLayout.PREFERRED_SIZE, 161, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel5, javax.swing.GroupLayout.PREFERRED_SIZE, 33, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(txtUsuario, javax.swing.GroupLayout.PREFERRED_SIZE, 33, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel6, javax.swing.GroupLayout.PREFERRED_SIZE, 33, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(cbnTipo, javax.swing.GroupLayout.PREFERRED_SIZE, 33, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(txtFecha, javax.swing.GroupLayout.PREFERRED_SIZE, 33, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel8, javax.swing.GroupLayout.PREFERRED_SIZE, 33, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(9, 9, 9)
                .addComponent(jSeparator2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(10, 10, 10)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.CENTER)
                    .addComponent(btnListar, javax.swing.GroupLayout.PREFERRED_SIZE, 35, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(cbnRecepciona, javax.swing.GroupLayout.PREFERRED_SIZE, 30, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel3, javax.swing.GroupLayout.PREFERRED_SIZE, 30, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(cbnEmite, javax.swing.GroupLayout.PREFERRED_SIZE, 30, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel1, javax.swing.GroupLayout.PREFERRED_SIZE, 30, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel2)
                    .addComponent(jLabel4))
                .addGap(8, 8, 8)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addComponent(jScrollPane2, javax.swing.GroupLayout.PREFERRED_SIZE, 284, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 284, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                        .addComponent(btnRegistrar, javax.swing.GroupLayout.PREFERRED_SIZE, 55, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addComponent(btnOcurrencias, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                    .addComponent(jButton1))
                .addContainerGap())
        );

        btnRegistrar.getAccessibleContext().setAccessibleName("<HTML>REGISTRAR <BR>ENVIO</HTML>");

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void cbnEmiteActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_cbnEmiteActionPerformed
        if (cbnEmite.getSelectedIndex() == cbnRecepciona.getSelectedIndex()) {
            cbnEmite.setSelectedIndex(0);
            limpiarTablaEmisor();
            limpiarTablaReceptor();
        } else {
            validarTablasEmisor();
            validarBotones();
        }
        if (cbnEmite.getSelectedIndex() == 0) {
            limpiarTablaEmisor();
        }
    }//GEN-LAST:event_cbnEmiteActionPerformed

    private void cbnRecepcionaActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_cbnRecepcionaActionPerformed
        if (cbnEmite.getSelectedIndex() == cbnRecepciona.getSelectedIndex()) {
            cbnRecepciona.setSelectedIndex(0);
            validarBotones();
            limpiarTablaReceptor();
        } else {
//          validarTablasReceptor();
            validarBotones();
        }
        if (cbnRecepciona.getSelectedIndex() == 0) {
            validarBotones();
            btnListar.setEnabled(false);
        } else {
            btnListar.setEnabled(true);
        }
    }//GEN-LAST:event_cbnRecepcionaActionPerformed

    private void tblEmisorMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_tblEmisorMouseClicked
        if (evt.getClickCount() == 2) {
            if (cbnRecepciona.getSelectedIndex() != 0) {
                // System.out.print(cbnRecepciona.getSelectedIndex());
                int FilaSeleccionada = tblEmisor.getSelectedRow();
                new frmCantidadMovimiento(null, true, (MovimientoM) tblEmisor.getValueAt(FilaSeleccionada, 3)).setVisible(true);
            } else {
                JOptionPane.showMessageDialog(null, "Selecione tienda destino.");
            }
        } else {
        }
    }//GEN-LAST:event_tblEmisorMouseClicked

    private void tblReceptorMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_tblReceptorMouseClicked
        if (tblReceptor.getSelectedColumn() == 4) {
            if (tblReceptor.getRowCount() != 0) {
                oModelTblas.removeRow(tblReceptor.getSelectedRow());
                // calcularImporte();
            }
        }

        if (tblReceptor.getRowCount() != 0) {
            cbnRecepciona.setEnabled(false);
        } else {
            cbnRecepciona.setEnabled(true);
        }
    }//GEN-LAST:event_tblReceptorMouseClicked

    @SuppressWarnings({"UseSpecificCatch", "CallToThreadDumpStack"})
    private void btnRegistrarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnRegistrarActionPerformed
        if (tblReceptor.getRowCount() == 0) {
            JOptionPane.showMessageDialog(rootPane, "No se han agregado envios por registrar. Verifique...");
        } else {
            int rsp = JOptionPane.showConfirmDialog(null, "¿Desea registrar los envios?", "Mensaje de Dialogo", 0);
            if (rsp == 0 && tblReceptor.getRowCount() > 0) {
                SimpleDateFormat formateador = new SimpleDateFormat("dd-MM-yyyy HH:mm:ss", new Locale("EN"));
                Date fechaDate = new Date();
                String fecha = formateador.format(fechaDate);
                System.out.println(fecha + " : "+fecha.length());
                MovimientoM oMovimientoM = new MovimientoM();
                oMovimientoM.setEmite(cbnEmite.getSelectedIndex());
                oMovimientoM.setRecepciona(cbnRecepciona.getSelectedIndex());
                oMovimientoM.setTipoSalida(cbnTipo.getSelectedItem().toString());
                oMovimientoM.setIdTiendaregistro(xidTienda);
                oMovimientoM.setFechaCompleta(fecha);
                try {
                    int error = 0;
                    Random numerosAleatorios = new Random();
                    int cara;
                    do {
                        cara = numerosAleatorios.nextInt(1000000000);
                        if ((cara + "").length() == 9) {
                            if (!encontrarCodigo(cara + "")) {
                                oMovimientoM.setIdBloque(cara + "");
                                       System.out.println(">> "+cara);
                            }
                        }
                    } while ((cara + "").length() != 9 && encontrarCodigo(cara + "") );

                    for (int i = 0; i < tblReceptor.getRowCount(); i++) {
                        oMovimientoM.setCantidad(Integer.parseInt(tblReceptor.getValueAt(i, 2).toString().trim()));
                        oMovimientoM.setIdprenda(Integer.parseInt(tblReceptor.getValueAt(i, 0).toString().trim()));
                     //   System.out.println(":. "+oMovimientoM.getIdBloque());
                        MovimientoC oC = new MovimientoC();
                        if (oC.agregarMovimiento(oMovimientoM, cbnTipo.getSelectedItem().toString())) {
                            error = 1;
                        }
                        if ("ENTREGAS".equals(cbnTipo.getSelectedItem().toString())) {
                            new VentaDetalleC().actualizarStock(cbnRecepciona.getSelectedIndex(), Integer.parseInt(tblReceptor.getValueAt(i, 0).toString().trim()));
                            new VentaDetalleC().actualizarStock(cbnEmite.getSelectedIndex(), Integer.parseInt(tblReceptor.getValueAt(i, 0).toString().trim()));
                        } else {
                            new VentaDetalleC().actualizarStock(cbnRecepciona.getSelectedIndex(), Integer.parseInt(tblReceptor.getValueAt(i, 0).toString().trim()));
                            new VentaDetalleC().actualizarStock(cbnEmite.getSelectedIndex(), Integer.parseInt(tblReceptor.getValueAt(i, 0).toString().trim()));
                            new VentaDetalleC().actualizarStockDevoluciones(cbnRecepciona.getSelectedIndex(), Integer.parseInt(tblReceptor.getValueAt(i, 0).toString().trim()));
                            new VentaDetalleC().actualizarStockDevoluciones(cbnEmite.getSelectedIndex(), Integer.parseInt(tblReceptor.getValueAt(i, 0).toString().trim()));
                        }
                    }
                    if (error == 0) {
                        JOptionPane.showMessageDialog(null, "Se realizó la operación con exito.");
                        cbnEmite.setSelectedIndex(0);
                        cbnRecepciona.setSelectedIndex(0);

                        limpiarTablaEmisor();
                        limpiarTablaReceptor();
                    }
                } catch (Exception e) {
                    e.printStackTrace();
                }
                if (tblReceptor.getRowCount() != 0) {
                    cbnRecepciona.setEnabled(false);
                } else {
                    cbnRecepciona.setEnabled(true);
                }

                // frmVenta.cargarModelo(xtienda);
            }
        }
    }//GEN-LAST:event_btnRegistrarActionPerformed

    private boolean encontrarCodigo(String v) {
        try {
            MovimientoC c = new MovimientoC();
            int rsp = c.buscarCodigo(v);
            if (rsp != 0) {
                return true;
            } else {
                return false;
            }
        } catch (Exception e) {
            e.printStackTrace();
            return true;
        }

    }

    private void btnListarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnListarActionPerformed
        frmListaPrendas oListaPrendas = new frmListaPrendas(null, true, cbnRecepciona.getSelectedIndex(), cbnRecepciona.getSelectedItem().toString());
        oListaPrendas.setVisible(true);

    }//GEN-LAST:event_btnListarActionPerformed

    private void tblEmisorKeyReleased(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_tblEmisorKeyReleased
        // TODO add your handling code here:
    }//GEN-LAST:event_tblEmisorKeyReleased

    private void btnNuevaActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnNuevaActionPerformed
        new frmModelo(null, true).setVisible(true);
    }//GEN-LAST:event_btnNuevaActionPerformed

    private void btnOcurrenciasActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnOcurrenciasActionPerformed
        new frmOcurrencias(null, true, xidTienda).setVisible(true);
    }//GEN-LAST:event_btnOcurrenciasActionPerformed

    private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton1ActionPerformed
        new frmVerMovimientosBloque(null, true, txtUsuario.getText(), oTienda.getNomTienda(), xidTienda).setVisible(true);
    }//GEN-LAST:event_jButton1ActionPerformed
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton btnHistorial;
    private javax.swing.JButton btnListar;
    private javax.swing.JButton btnNueva;
    private javax.swing.JButton btnOcurrencias;
    private javax.swing.JButton btnRegistrar;
    public static javax.swing.JComboBox cbnEmite;
    public static javax.swing.JComboBox cbnRecepciona;
    private javax.swing.JComboBox cbnTipo;
    private javax.swing.JButton jButton1;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JLabel jLabel5;
    private javax.swing.JLabel jLabel6;
    private javax.swing.JLabel jLabel7;
    private javax.swing.JLabel jLabel8;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JScrollPane jScrollPane2;
    private javax.swing.JSeparator jSeparator2;
    public static javax.swing.JTable tblEmisor;
    public static javax.swing.JTable tblReceptor;
    private javax.swing.JTextField txtFecha;
    private javax.swing.JTextField txtUsuario;
    // End of variables declaration//GEN-END:variables

    private void cargarTiendas() {
        TiendaC oC = new TiendaC();
        List<TiendaM> oMs = oC.listarTodo();
        @SuppressWarnings("LocalVariableHidesMemberVariable")
        String[] oList;
        if (oMs != null) {
            int size = oMs.size();
            oList = new String[size + 1];
            for (int a = 0; a < size; a++) {
                oList[0] = "----------------------";
                oList[a + 1] = oMs.get(a).getNomTienda();
            }
            tiendaEmisor = oList;
            tiendaReceptor = oList;
        }

    }

    private void cargarTaller() {
        MovimientoC oC = new MovimientoC();
        List<MovimientoM> oMs = oC.listarPrendasTaller();
        @SuppressWarnings("LocalVariableHidesMemberVariable")
        ArrayList oList;
        limpiarTablaEmisor();
        if (oMs != null) {
            int size = oMs.size();
            for (int a = 0; a < size; a++) {
                oList = new ArrayList();
                oList.add(oMs.get(a).getCantidad());
                oList.add(" " + oMs.get(a).getNombrePrenda());
                oList.add(oMs.get(a).getPrecio());
                oList.add(oMs.get(a));
                ((ArrayListTableModel) tblEmisor.getModel()).addRow(oList);
            }
        }
    }

    private void limpiarTablaEmisor() {
        ArrayListTableModel oModelTbl = (ArrayListTableModel) tblEmisor.getModel();
        int i = oModelTbl.getRowCount();
        for (int a = 0; a < i; a++) {
            oModelTbl.removeRow(0);
        }
    }

    private void validarTablasEmisor() {
        switch (cbnEmite.getSelectedIndex()) {
            case 1:
                cargarTaller();
                break;
            case 2:
                cargarTienda(2);
                break;
            case 3:
                cargarTienda(3);
                break;
        }
    }

    private void configurarTabla() {
        DefaultTableCellRenderer tcr = new DefaultTableCellRenderer();
        tcr.setHorizontalAlignment(SwingConstants.CENTER);

        tblEmisor.getColumnModel().getColumn(0).setCellRenderer(tcr);
        tblEmisor.getColumnModel().getColumn(2).setCellRenderer(tcr);

        tblEmisor.getColumnModel().getColumn(0).setMinWidth(50);
        tblEmisor.getColumnModel().getColumn(0).setPreferredWidth(50);
        tblEmisor.getColumnModel().getColumn(0).setMaxWidth(50);
        tblEmisor.getColumnModel().getColumn(2).setMinWidth(70);
        tblEmisor.getColumnModel().getColumn(2).setPreferredWidth(70);
        tblEmisor.getColumnModel().getColumn(2).setMaxWidth(70);
        tblEmisor.getColumnModel().getColumn(3).setMinWidth(0);
        tblEmisor.getColumnModel().getColumn(3).setPreferredWidth(0);
        tblEmisor.getColumnModel().getColumn(3).setMaxWidth(0);

        tblReceptor.getColumnModel().getColumn(0).setCellRenderer(tcr);
        tblReceptor.getColumnModel().getColumn(2).setCellRenderer(tcr);
        tblReceptor.getColumnModel().getColumn(3).setCellRenderer(tcr);

        tblReceptor.getColumnModel().getColumn(0).setMinWidth(0);
        tblReceptor.getColumnModel().getColumn(0).setPreferredWidth(0);
        tblReceptor.getColumnModel().getColumn(0).setMaxWidth(0);
        tblReceptor.getColumnModel().getColumn(1).setMinWidth(150);
        tblReceptor.getColumnModel().getColumn(1).setPreferredWidth(150);
        tblReceptor.getColumnModel().getColumn(1).setMaxWidth(150);
        tblReceptor.getColumnModel().getColumn(4).setMinWidth(50);
        tblReceptor.getColumnModel().getColumn(4).setPreferredWidth(50);
        tblReceptor.getColumnModel().getColumn(4).setMaxWidth(50);
        tblReceptor.getColumnModel().getColumn(5).setMinWidth(0);
        tblReceptor.getColumnModel().getColumn(5).setPreferredWidth(0);
        tblReceptor.getColumnModel().getColumn(5).setMaxWidth(0);

        tblReceptor.getColumnModel().getColumn(4).setCellRenderer(new MiRenderBoton(true));
    }

    private void validarBotones() {

        if (cbnRecepciona.getSelectedIndex() == 1) {
            cbnTipo.setSelectedIndex(1);
        } else {
            cbnTipo.setSelectedIndex(0);
        }
    }

    private void limpiarTablaReceptor() {
        try {
            ArrayListTableModel oModelTbl = (ArrayListTableModel) tblReceptor.getModel();
            int i = oModelTbl.getRowCount();
            for (int a = 0; a < i; a++) {
                oModelTbl.removeRow(0);
            }
        } catch (Exception e) {
        }
    }

    private void cargarTienda(int x) {
        MovimientoC oC = new MovimientoC();
        List<MovimientoM> oMs = oC.listarPrendasTienda(x);
        @SuppressWarnings("LocalVariableHidesMemberVariable")
        ArrayList oList;
        limpiarTablaEmisor();
        if (oMs != null) {
            int size = oMs.size();
            for (int a = 0; a < size; a++) {
                oList = new ArrayList();
                oList.add(oMs.get(a).getCantidad());
                oList.add(" " + oMs.get(a).getNombrePrenda());
                oList.add(oMs.get(a).getPrecio());
                oList.add(oMs.get(a));
                ((ArrayListTableModel) tblEmisor.getModel()).addRow(oList);
            }
        }

    }
}
